package com.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.CheckGroup;
import com.health.service.CheckGroupService;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;
    /**
     * 编辑检查组
     */
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) {
        int count = checkGroupService.update(checkGroup, checkItemIds);
        if (count > 0) {
            return new Result(true, "编辑检查组成功");
        }

        return new Result(false, "编辑检查组失败");
    }
    /**
     * 根据检查组ID查询关联到的检查项ID
     */
    @GetMapping("/findCheckItemIds/{id}")
    public Result findCheckItemIds(@PathVariable Integer id) {
        List<Integer> checkItemIds = checkGroupService.findCheckItemIds(id);

        return new Result(true, "查询关联的检查项成功", checkItemIds);
    }
    /**
     * 根据ID查询检查组
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        CheckGroup checkGroup = checkGroupService.findById(id);

        return new Result(true, "查询检查组成功", checkGroup);
    }

    /**
     * 新增检查组
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) {
        int count = checkGroupService.add(checkGroup, checkItemIds);
        if (count > 0) {
            return new Result(true, "新增检查组成功");
        }

        return new Result(false, "新增检查组失败");
    }
    /**
     * 删除检查组
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        int count = checkGroupService.deleteById(id);
        if (count > 0) {
            return new Result(true, "删除检查组成功");
        }

        return new Result(false, "删除检查组失败");
    }
    /**
     * 检查组分页条件查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    public Result findPage(@PathVariable Integer pageNow, @PathVariable Integer pageSize, String query) {
        PageResult<CheckGroup> pageResult = checkGroupService.findPage(pageNow, pageSize, query);

        //封装返回结果
        return new Result(true, "检查组分页查询成功", pageResult);
    }
}